package com.face.server.convert;

import com.face.server.dao.JoinedDAO;
import com.face.server.entity.EventLog;
import com.face.server.entity.Visitor;
import com.face.server.vo.VisitorEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ryan on 2018/4/12.
 */
@Component
public class Event2VisitorEvent {
    @Autowired
    JoinedDAO joinedDAO;
    public  VisitorEvent convert(EventLog eventLog){
        VisitorEvent visitorEvent=new VisitorEvent();
        BeanUtils.copyProperties(eventLog,visitorEvent);
        Visitor visitor=joinedDAO.findByNameAndType(eventLog.getName(),eventLog.getType());
        String stuName=null;
        if (visitor==null||visitor.getStudent()==null)
            stuName="";
        else
            stuName=visitor.getStudent().getName();
        visitorEvent.setStuName(stuName);
        return visitorEvent;
    }

    public List<VisitorEvent> convert(List<EventLog> eventLogs){
      return eventLogs.stream().map(e->convert(e)).collect(Collectors.toList());
    }
}
